freeslot("MT_PURPLEGRAPE_DECOR", "MT_PURPLEGRAPE")
freeslot("MT_PURPLEGRAPE_PARTICLE", "MT_PURPLEGRAPE_MISSILE")

function A_GrapeTripleShot(actor, var1, var2)
local grape1 = P_SpawnMobjFromMobj(actor, 0, 0, 0, MT_PURPLEGRAPE_MISSILE)
local grape2 = P_SpawnMobjFromMobj(actor, 0, 0, 0, MT_PURPLEGRAPE_MISSILE)
	if grape1
	and grape1.valid
	grape1.state = S_PURPLEGRAPE_MISSILE3
	grape1.angle = actor.angle+ANG20
	grape1.target = actor.target
	P_InstaThrust(grape1, grape1.angle, FixedHypot(actor.momx, actor.momy))
	grape1.momz = actor.momz
	end
	if grape2
	and grape2.valid
	grape2.state = S_PURPLEGRAPE_MISSILE3
	grape2.angle = actor.angle-ANG20
	grape2.target = actor.target
	P_InstaThrust(grape2, grape2.angle, FixedHypot(actor.momx, actor.momy))
	grape2.momz = actor.momz
	end
end

function A_GrapeCeilBounce(actor, var1, var2) //SOC Hacks
	if actor.momz <= 0
	actor.flags = $1 & ~MF_GRENADEBOUNCE
	end
end

addHook("TouchSpecial", function(grape, mo)
	if grape
	and grape.valid
	and grape.health
		if mo
		and mo.valid
		and mo.player
		and mo.player.playerstate == PST_LIVE
			if mo.juicehud < 2*TICRATE
			mo.juicehud = 5*TICRATE
				if mo.juicescale >= FRACUNIT
				mo.juicescale = FRACUNIT+FRACUNIT/2
				end
			S_StartSound(grape, sfx_ghit)
			P_KillMobj(grape)
			end
		end
	end
return true
end, MT_PURPLEGRAPE_PARTICLE)

addHook("MobjDeath", function(mo)
	if mo
	and mo.valid
	local prandom = P_RandomRange(1, 8)
		if prandom == 1
		S_StartSound(mo, sfx_wdrip1)
		elseif prandom == 2
		S_StartSound(mo, sfx_wdrip2)
		elseif prandom == 3
		S_StartSound(mo, sfx_wdrip3)
		elseif prandom == 4
		S_StartSound(mo, sfx_wdrip4)
		elseif prandom == 5
		S_StartSound(mo, sfx_wdrip5)
		elseif prandom == 6
		S_StartSound(mo, sfx_wdrip6)
		elseif prandom == 7
		S_StartSound(mo, sfx_wdrip7)
		elseif prandom == 8
		S_StartSound(mo, sfx_wdrip8)
		end
	end
end, MT_PURPLEGRAPE_PARTICLE)

addHook("TouchSpecial", function(grape, mo)
	if grape
	and grape.valid
	and grape.health
		if mo
		and mo.valid
		and mo.player
		and mo.player.playerstate == PST_LIVE
		mo.juicehud = 5*TICRATE
			if mo.juicescale >= FRACUNIT
			mo.juicescale = FRACUNIT+FRACUNIT/2
			end
			for i = 0, 12
			local particle = P_SpawnMobjFromMobj(grape, P_RandomRange(-8, 8)*FRACUNIT, P_RandomRange(-8, 8)*FRACUNIT, P_RandomRange(-8, 8)*FRACUNIT, MT_PURPLEGRAPE_PARTICLE)
				if particle
				and particle.valid
				P_SetObjectMomZ(particle, P_RandomRange(1, 8)*FRACUNIT)
				P_InstaThrust(particle, P_RandomRange(0, 360)*ANG1, P_RandomRange(1, 8)*FRACUNIT)
				end
			end
		local thok = P_SpawnMobjFromMobj(grape, 0, 0, 0, MT_THOK)
			if thok
			and thok.valid
			thok.state = S_PURPLEGRAPE_GRASS
			thok.fuse = -1
			end
		end
	end
end, MT_PURPLEGRAPE_DECOR)

addHook("MobjThinker", function(mo)
	if mo
	and mo.valid
	and mo.type == MT_PURPLEGRAPE
	and mo.health
		if mo.state == S_PURPLEGRAPE_FOUND
		or mo.state == S_PURPLEGRAPE_READYFALL
			if (leveltime % 4) == 0
			or (leveltime % 4) == 1
			mo.spritexoffset = 2*FRACUNIT
			else
			mo.spritexoffset = -2*FRACUNIT
			end
		else
		mo.spritexoffset = 0
		end
		if mo.state == S_PURPLEGRAPE_QUIET
		A_FindTarget(mo, MT_PLAYER)
			if mo.target
			and mo.target.valid
			local target = mo.target
				if R_PointToDist2(mo.x, mo.y, target.x, target.y) <= 512*FRACUNIT
				and P_CheckSight(mo, target)
				mo.state = S_PURPLEGRAPE_FOUND
				end
			end
		elseif mo.state == S_PURPLEGRAPE_FOUND
			if (leveltime % 8) == 0
			S_StartSound(mo, sfx_s22b)
			end
		elseif mo.state == S_PURPLEGRAPE_APEAR
		A_FindTarget(mo, MT_PLAYER)
			if mo.fallcooldown == nil
			S_StartSound(mo, sfx_cdfm16)
			mo.fallcooldown = TICRATE
			else
				if mo.fallcooldown > 0
				mo.fallcooldown = $1 - 1
				elseif mo.fallcooldown <= 0
					if mo.target
					and mo.target.valid
					local target = mo.target
						if R_PointToDist2(mo.x, mo.y, target.x, target.y) <= 256*FRACUNIT
						and P_CheckSight(mo, target)
						mo.state = S_PURPLEGRAPE_READYFALL
						end
					end
				end
			end
		elseif mo.state == S_PURPLEGRAPE_READYFALL
			if (leveltime % 8) == 0
			S_StartSound(mo, sfx_s22b)
			end
			if mo.tics == 2
			local thok = P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_THOK)
				if thok
				and thok.valid
				thok.state = S_PURPLEGRAPE_GRASS
				thok.fuse = -1
				end
			end
		elseif mo.state == S_PURPLEGRAPE_FALLING
		mo.flags = $1 & ~MF_NOGRAVITY
			if P_IsObjectOnGround(mo)
			P_KillMobj(mo)
			end
		end
	end
end, MT_PURPLEGRAPE)

addHook("MobjDeath", function(mo, inf)
	if mo
	and mo.valid
	S_StartSound(mo, mo.info.deathsound)
		if inf
		and inf.valid
		and inf.player
		inf.juicehud = 5*TICRATE
			if inf.juicescale >= FRACUNIT
			inf.juicescale = FRACUNIT+FRACUNIT/2
			end
		end
		if mo.state != S_PURPLEGRAPE_FALLING
		local thok = P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_THOK)
			if thok
			and thok.valid
			thok.state = S_PURPLEGRAPE_GRASS
			thok.fuse = -1
			end
		end
		for i = 0, 12
		local particle = P_SpawnMobjFromMobj(mo, P_RandomRange(-8, 8)*FRACUNIT, P_RandomRange(-8, 8)*FRACUNIT, P_RandomRange(-8, 8)*FRACUNIT, MT_PURPLEGRAPE_PARTICLE)
			if particle
			and particle.valid
			P_SetObjectMomZ(particle, P_RandomRange(1, 8)*FRACUNIT)
			P_InstaThrust(particle, P_RandomRange(0, 360)*ANG1, P_RandomRange(1, 8)*FRACUNIT)
			end
		end
	end
end, MT_PURPLEGRAPE)

addHook("MobjDamage", function(mo, inf)
	if mo
	and mo.valid
	and mo.player
		if inf
		and inf.valid
			if inf.type == MT_PURPLEGRAPE
			P_DoPlayerPain(mo.player, inf, inf)
			S_StartSound(mo, sfx_altdi1)
			return true
			end
		end
	end
end, MT_PLAYER)

//Juice Effect on your screen
addHook("PlayerThink", function(player)
	if player.mo
	and player.mo.valid
		if player.mo.juicescale == nil
		player.mo.juicescale = 0
		end
		if player.mo.juicehud == nil
		player.mo.juicehud = 0
		else
			if player.mo.juicehud > 0
			player.mo.juicehud = $1 - 1
				if player.mo.juicescale > FRACUNIT
				player.mo.juicescale = $1 - FRACUNIT/16
				else
				player.mo.juicescale = min(FRACUNIT, $1+FRACUNIT/8)
				end
			else
			player.mo.juicescale = 0
			end
		end
	end
end)

local function GrapeJuiceScreenEffect(v, player, camera)
	if player.mo
	and player.mo.valid
		if player.mo.juicehud > 0
		local trans_flags = 0
			if player.mo.juicehud < 45
			and player.mo.juicehud >= 40
			trans_flags = V_10TRANS
			elseif player.mo.juicehud < 40
			and player.mo.juicehud >= 35
			trans_flags = V_20TRANS
			elseif player.mo.juicehud < 35
			and player.mo.juicehud >= 30
			trans_flags = V_30TRANS
			elseif player.mo.juicehud < 30
			and player.mo.juicehud >= 25
			trans_flags = V_40TRANS
			elseif player.mo.juicehud < 25
			and player.mo.juicehud >= 20
			trans_flags = V_50TRANS
			elseif player.mo.juicehud < 20
			and player.mo.juicehud >= 15
			trans_flags = V_60TRANS
			elseif player.mo.juicehud < 15
			and player.mo.juicehud >= 10
			trans_flags = V_70TRANS
			elseif player.mo.juicehud < 10
			and player.mo.juicehud >= 5
			trans_flags = V_80TRANS
			elseif player.mo.juicehud < 5
			and player.mo.juicehud >= 0
			trans_flags = V_90TRANS
			end
		local y_offset = (player.mo.juicehud/15)*FRACUNIT
		v.drawScaled(160*FRACUNIT, 100*FRACUNIT-y_offset, player.mo.juicescale, v.cachePatch("GRAPESCR"), trans_flags)
		end
	end
end

hud.add(GrapeJuiceScreenEffect, "game")